﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Valor.DBUtility.MSSQLHelper;
using xTeam.HengLe.Entity;
using System.Data.SqlClient;
using System.Data;

namespace xTeam.Hengle.DataAccess {
    public class ClassifyDataAccess {
        private bool _isCN = true;

        public ClassifyDataAccess(string connectionString, string langName) {
            SQLProcedureHelper.ConnectionString = connectionString;
            this._isCN = langName.ToLower() == "en-us" ? false : true;
        }

        public int Save(EClassify classify) {
            int result = 0;
            SqlParameter[] para = 
            {
                 new SqlParameter() { ParameterName = "@ClassifyId", SqlDbType = SqlDbType.Int, Value = classify.ClassifyId },
                 new SqlParameter() { ParameterName = "@Title", SqlDbType = SqlDbType.NVarChar, Size = 50, Value = classify.Title },
                 new SqlParameter() { ParameterName = "@KeyWords", SqlDbType = SqlDbType.NVarChar, Size = 500, Value = classify.KeyWords },
                 new SqlParameter() { ParameterName = "@Description", SqlDbType = SqlDbType.NVarChar, Size = 500, Value = classify.Description },
                 new SqlParameter() { ParameterName = "@ParentId", SqlDbType = SqlDbType.Int, IsNullable = true, Value = classify.ParentId },
                 new SqlParameter() { ParameterName = "@Sequence", SqlDbType = SqlDbType.Int, Value = classify.Sequence },
                 new SqlParameter() { ParameterName = "@ModuleKey", SqlDbType = SqlDbType.NVarChar, Size = 50, Value = classify.ModuleKey },
                new SqlParameter() { ParameterName = "@Redirect", SqlDbType = SqlDbType.NVarChar, Size = 500, Value = classify.Redirect }
            };
            result = SQLProcedureHelper.ExecuteNonQuery(this._isCN ? "classify_update" : "classify_update_en", para);

            return result;
        }

        public int Delete(int classifyId) {
            int result = 0;
            SqlParameter[] para = { new SqlParameter("@ClassifyId", SqlDbType.NVarChar) { Value = classifyId } };
            result = SQLProcedureHelper.ExecuteNonQuery(this._isCN ? "classify_delete" : "classify_delete_en", para);
            return result;
        }

        public EClassify GetClassifyById(int classifyId) {
            EClassify classify = null;

            SqlParameter[] para = { new SqlParameter("@ClassifyId", SqlDbType.NVarChar) { Value = classifyId } };
            SqlDataReader reader = SQLProcedureHelper.ExecuteReader(this._isCN ? "classify_select" : "classify_select_en", para);

            if (reader.HasRows) {
                classify = new EClassify();
                reader.Read();
                classify.ClassifyId = Convert.ToInt32(reader["ClassIfyId"]);
                classify.Title = reader["Title"].ToString();
                classify.Description = reader["Description"].ToString();
                classify.KeyWords = reader["KeyWords"].ToString();
                classify.ModuleKey = reader["ModuleKey"].ToString();
                classify.ParentId = Convert.ToInt32(reader["ParentId"]);
                classify.Redirect = reader["Redirect"].ToString();
                classify.Sequence = Convert.ToInt32(reader["Sequence"]);
            }

            return classify;
        }

        public List<EClassify> GetClassLisytByModuleKey(string moduleKey) {
            List<EClassify> classifyList = new List<EClassify>();
            SqlParameter[] para = 
            {
                new SqlParameter("@ModuleKey", SqlDbType.NVarChar) { Value = moduleKey }
            };
            SqlDataReader reader = SQLProcedureHelper.ExecuteReader(this._isCN ? "classify_selectList" : "classify_selectList_en", para);
            while (reader.Read()) {
                classifyList.Add(new EClassify() {
                    ClassifyId = Convert.ToInt32(reader["ClassIfyId"]),
                    Title = reader["Title"].ToString(),
                    KeyWords = reader["KeyWords"].ToString(),
                    Description = reader["Description"].ToString(),
                    ParentId = Convert.ToInt32(reader["ParentId"]),
                    Path = reader["Path"].ToString(),
                    Sequence = Convert.ToInt32(reader["Sequence"]),
                    Floor = Convert.ToInt32(reader["Floor"]),
                    Redirect = reader["Redirect"].ToString(),
                    ModuleKey = reader["ModuleKey"].ToString()
                });
            }
            reader.Close();
            return classifyList;
        }

        public List<EClassify> GetParentList(int classId, string moduleKey) {
            List<EClassify> classifyList = new List<EClassify>();
            SqlDataReader reader = SQLProcedureHelper.ExecuteReader(this._isCN ? "Classify_SelectParent" : "Classify_SelectParent_en"
                , new SqlParameter("@ModuleKey", SqlDbType.NVarChar) { Value = moduleKey }
                , new SqlParameter("@ClassifyId", SqlDbType.NVarChar) { Value = classId });
            while (reader.Read()) {
                classifyList.Add(new EClassify() {
                    ClassifyId = Convert.ToInt32(reader["ClassIfyId"]),
                    Title = reader["Title"].ToString(),
                    KeyWords = reader["KeyWords"].ToString(),
                    Description = reader["Description"].ToString(),
                    ParentId = Convert.ToInt32(reader["ParentId"]),
                    Path = reader["Path"].ToString(),
                    Sequence = Convert.ToInt32(reader["Sequence"]),
                    Floor = Convert.ToInt32(reader["Floor"]),
                    ModuleKey = reader["ModuleKey"].ToString()
                });
            }
            reader.Close();
            return classifyList;
        }
    }
}
